package com.woniu.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.entity.PageData;
import com.woniu.entity.Result;
import com.woniu.entity.dto.CollectDTO;
import com.woniu.entity.dto.QueryTrainingDTO;
import com.woniu.entity.dto.QueryTrainingLevelDTO;
import com.woniu.entity.vo.CollectVO;
import com.woniu.pojo.Collect;
import com.woniu.pojo.Training;
import com.woniu.pojo.TrainingLevel;
import com.woniu.service.ITrainingLevelService;
import com.woniu.service.ITrainingService;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.runtime.Undefined;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/trainingLevel")
public class TrainingLevelController extends BaseController{
    @Autowired
    private ITrainingLevelService trainingLevelService;

    @ApiOperation("分页+搜索")
    @PostMapping("/listSearch/{currentPage}/{pageSize}")
    public Result listSearch(@PathVariable Integer currentPage,
                             @PathVariable Integer pageSize,
                             @RequestBody QueryTrainingLevelDTO queryTrainingLevelDTO) {
//        将靶场类型转换成对应的parentId
        if (!StringUtils.isEmpty(queryTrainingLevelDTO.getTrainingType()) &&
                !queryTrainingLevelDTO.getTrainingType().equals("0")){
            System.out.println("111");
            queryTrainingLevelDTO.setTrainingTypeChange(getParentId(queryTrainingLevelDTO.getTrainingType()));
        }
        if (!StringUtils.isEmpty(queryTrainingLevelDTO.getTrainingType()) &&queryTrainingLevelDTO.getTrainingType().equals("0")){
            queryTrainingLevelDTO.setTrainingTypeChange(0);
        }
        PageData pageData = trainingLevelService.listLevel(currentPage, pageSize, queryTrainingLevelDTO);
        return toDataResult(pageData);
    }


    @ApiOperation("靶场种类获取ParentId")
    public Integer getParentId(String pName){
        Integer parentId = trainingLevelService.getParentId(pName);
        return parentId;
    }

    @ApiOperation("新增或修改")
    @PostMapping("/insertOrUpdate")
    public Result insertOrUpdate(@RequestBody TrainingLevel trainingLevel) {
        Boolean b = null;
        if (StringUtils.isEmpty(trainingLevel.getId())) {
            b = trainingLevelService.save(trainingLevel);
            trainingLevelService.setTid(trainingLevel.getId());
        } else {
            b = trainingLevelService.updateById(trainingLevel);
        }
        return toResult(b);
    }

    @ApiOperation("ID删除")
    @PostMapping("/delete/{id}")
    public Result deleteTrainingLevel(@PathVariable Integer id) {
        boolean b = trainingLevelService.removeById(id);
        return toResult(b);
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody Integer[] ids) {
        boolean b = trainingLevelService.removeBatchByIds(Arrays.asList(ids));
        return toResult(b);
    }

    @ApiOperation("根据ID修改")
    @PostMapping("/update")
    public Result updateTrainingLevel(@RequestBody TrainingLevel trainingLevel) {
        boolean b = trainingLevelService.updateById(trainingLevel);
        return toResult(b);
    }

    @ApiOperation("根据ID查询")
    @GetMapping("/select/{id}")
    public Result selectById(@PathVariable Integer id) {
        QueryWrapper<TrainingLevel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        List<TrainingLevel> list = trainingLevelService.list(queryWrapper);
        return toDataResult(list);
    }

    @ApiOperation("查询所有")
    @GetMapping("/list")
    public Result listAll() {
        List<TrainingLevel> list = trainingLevelService.list();
        return toDataResult(list);
    }

    @ApiOperation("标题校验")
    @GetMapping("/nameOnly/{name}")
    public Result nameIsExist(@PathVariable String name){
        Boolean flag = trainingLevelService.nameIsExist(name);
        return toDataResult(flag);
    }
    @ApiOperation("标题校验")
    @GetMapping("/nameOnly2/{name}")
    public Result nameIsExist2(@PathVariable String name){
        Boolean flag = trainingLevelService.nameIsExist2(name);
        return toDataResult(flag);
    }
    @ApiOperation("id校验")
    @GetMapping("/idOnly/{id}")
    public Result iDIsExist(@PathVariable Integer id){
        Boolean flag = trainingLevelService.idIsExist(id);
        return toDataResult(flag);
    }

    @ApiOperation("导出数据")
    @PostMapping("/export")
    public void exportExcel(@RequestBody(required = false) QueryTrainingLevelDTO queryTrainingLevelDTO,
                            HttpServletResponse response) throws IOException {
        PageData pageData = trainingLevelService.listSearch(1, 1000, queryTrainingLevelDTO);
        List<TrainingLevel> trainingLevelList = (List<TrainingLevel>) pageData.getData();
        EasyExcel.write(response.getOutputStream(), TrainingLevel.class).sheet().doWrite(trainingLevelList);
    }
}
